package com.johnny.xia.webcollector.base;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SqlliteUtil {
	
	private static final Logger logger =LoggerFactory.getLogger(SqlliteUtil.class);

	public static final String db_url = "jdbc:sqlite:heheda.db";
	
	private static Connection connection = null;
	
	static{
		try {
			Class.forName("org.sqlite.JDBC");
			connection = DriverManager.getConnection(db_url);
			//logger.error("open sqllite db success~~~~~~~");
			System.out.println("open sqllite db success~~~~~~~");
			createTable();
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static void createTable() throws SQLException{
//		String createTable="create table if not exists images(id INTEGER primary key autoincrement,store,url)";
		String createTable="create table if not exists images(url primary key,store)";
		Statement stmt = connection.createStatement();
		stmt.executeUpdate(createTable);
		stmt.close();
		System.out.println("create table success~~~~~~~~~~");
	}
	
	public static void insertImage(String url,String store) throws SQLException{
//		String insertSql="insert into images(url,store) values ('"+url+"','"+store+"')" ;
		String insertSql="replace into images(url,store) values ('"+url+"','"+store+"')" ;
		Statement stmt = connection.createStatement();
		stmt.executeUpdate(insertSql);
		stmt.close();
	}
	
	public static void main(String[] args) throws SQLException {
		insertImage("xxx", "test");
	}
	
	
}
